<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 7/18/2016
 * Des: 系统管理员
 */
defined('INHkMvc') or exit('Access Invalid!');

class App_member_balance_applyModel extends AppModel
{
    /**
     * 添加
     * @param $member_id
     * @param $amount
     * @return bool
     */
    public function add($member_id, $amount, $account,$member_integration_id=0)
    {
        if (intval($member_id) == 0) {
            return false;
        }
        $data = [];
        if($member_integration_id!=0){
           $member_integration=M('app.member_integration')->field('integral_voucher')->select(['id'=>['in',$member_integration_id]]);
           foreach ($member_integration as $k => $v) {
              $data['member_integration']+=$v['integral_voucher'];
           }
        }else{
            $data['member_integration']=0;
        }
        $data['member_id'] = intval($member_id);
        $data['amount'] = $amount+$data['member_integration'];
        $data['account_type'] = intval($this->account_type);
        $data['account_num'] = trim($account);
        $data['start_time'] = time();
        $data['member_integration_id'] = $member_integration_id;
        $data['amount_money'] =$amount ;
        $data['msg'] = 0;
        $data['running_no'] = $this->setRunningNo($data['member_id']);
        return $this->insert($data);
    }
    /**
     * 设置流水号
     * @param $member_id
     * @return string
     */
    private function setRunningNo($member_id)
    {
        $num = pow(10, 8) + $member_id;
        return 'BL' . date('m') . substr($num . hexdec(uniqid()), 1);
    }
    /**
     * 获取会员日志
     * @param $member_id
     * @param $status 状态(0:失败;1:成功;2:申请;3:授理;)
     * @param int $page
     * @param int $page_size
     * @return common
     */
    public function getLogListByMemberId($member_id, $status = '', $condition = [], $page = 1, $page_size = 20)
    {
        $condition[] = ['member_id' => $member_id];
        if ($status != '') {
            $condition[] = ['status' => $status];
        }
        return $this->getLogList($condition, $page, $page_size);
    }
    /**
     * 获取日志
     * @param $condition
     * @param int $page
     * @param int $page_size
     * @return common
     */
    public function getLogList($condition, $page = 1, $page_size = 20)
    {
        return $this->limit(abs($page - 1) * $page_size, $page_size)->select($condition);
    }
}
